package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chixing.entity.OrderExtension;
import com.chixing.mapper.OrderExtensionMapper;
import com.chixing.service.IOrderExtensionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-09-30
 */
@Service
public class OrderExtensionServiceImpl implements IOrderExtensionService {

    @Autowired
    private OrderExtensionMapper orderExtensionMapper;

    @Override
    public Result addExtension(OrderExtension orderExtension) {
        String extensionNo = UUID.randomUUID().toString().replace("-","");
        orderExtension.setExtensionTradeNo(extensionNo);
        orderExtension.setExtensionStatus("待支付");
        orderExtension.setApplyTime(LocalDateTime.now());
        int rows = orderExtensionMapper.insert(orderExtension);
        if (rows>0)
            return Result.saveSuccess(extensionNo);
        return Result.saveFail(null);
    }

    @Override
    public OrderExtension getByTradeNo(String tradeNo) {
        return orderExtensionMapper.selectOne(new QueryWrapper<OrderExtension>().eq("extension_trade_no", tradeNo));
    }

    @Override
    public boolean updateStatusById(Long extensionId, String status) {
        OrderExtension extension = new OrderExtension();
        extension.setExtensionId(extensionId);
        extension.setExtensionStatus(status);
        int rows = orderExtensionMapper.updateById(extension);
        return rows > 0;
    }

}
